package com.omd.client.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * 配置Handler等
 * @author yuejing
 * @date 2016年1月20日 下午10:00:56
 * @version V1.0.0
 */
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        p.addLast("decodec", new HttpRequestDecoder());
        p.addLast("encoder",new HttpResponseEncoder());
        // 为http request解析准备最大4MB缓存，似乎可以更大点
        // 小一些可以更安全的防护flood攻击
        p.addLast("aggegator",new HttpObjectAggregator(1024 * 1024 * 4));
        /*
         * 压缩
         * Compresses an HttpMessage and an HttpContent in gzip or deflate encoding
         * while respecting the "Accept-Encoding" header.
         * If there is no matching encoding, no compression is done.
         */
        p.addLast("deflater", new HttpContentCompressor());
        p.addLast("handler", new HttpServerHandler());
	}

}